/**
 * 我的订单
 * @author Qasim <15750783791@163.com>
 * @version 2016/9/28
 */
var memberInfo = '';
document.addEventListener('plusready' , ready ,  false);

function ready(){
	 
	memberInfo = plus.storage.getItem('memberInfo');
	
	memberInfo = $.parseJSON(memberInfo);
	//获取我的订单
	plus.nativeUI.showWaiting('发布中...'); 
		
	$.ajax({
		
		type:"post",
		 
		url:apiRoot +'/Home/Orders/my_orders/',
		
		data : {
			
			uid : memberInfo.aid 
			
		},
		dataType : 'json',
		success : function(data){
			
			plus.nativeUI.closeWaiting();
			
			if(data){
				$.each(data, function( k ,v) {
					var status = '';
					switch (parseInt(v.status)){
						
						case 0:
							status = '订单已取消';
							break; 
						case 1:
							status = '订单已生成,等待扫描发票';
							break;
						case 2:
							status = '已扫描发票,等待商家确认';
							break;
						case 3:
							status = '商家已确认,等待用户评价';
							break;
						case 4:
							status = '用户已评价,等待积分发放';
							break;
						case 5:
							status = '积分已发放,订单完成';
							break;
					}
					var html = '<div class="background-w margin-top-5 table-view table-view-cell order-list"><div class="order-list-2 order'+v.id+'" data-businessid="'+v.business_id+'" data-id="'+v.id+'" data-addtime="'+v.addtime+'" data-status='+v.status+'><li>';
						html += '<span class="color-zx font-13 name">'+v.business_name+'</span><span class="mui-pull-right color-c font-13 status'+v.id+'">'+status+'</span></li>';
						html += '<li class="order-h"><span class="mui-ellipsis-2 font-14 line-h-23 margin-top-5 details">'+v.business_details+'</span></li></div>';
						html += '<li class="margin-top-10"><span class="color-c font-13 addtime">'+v.addtime+'</span><span class="mui-pull-right order_status'+v.id+'">';
						if(v.status == 3 || v.status == 4 || v.status == 5){
							html += '<button class="background-zx color-white order-btn">已完成</button></span></li></div>';
						}else if(v.status == 0){
							html += '<button class="background-zx color-white order-btn">已取消</button></span></li></div>';
						}else{
							html += '<button class="background-zx color-white order-btn" onclick="cancel('+v.id+')">取消订单</button></span></li></div>';
						}
						
					$('.lists').append(html); 
				});
				$('.order-list-2').each(function(index){
					$(this).on('click' , function(){
						plus.nativeUI.showWaiting('跳转中...');
						var me_order_inf = plus.webview.create('me-order-inf.html', 'me_order_inf', {}, {
							aid : $(this).attr('data-id'),
							business_id :  $(this).attr('data-businessid'),
							addtime : $(this).attr('data-addtime'),
							name : $(this).find('.name').text(),
							details : $(this).find('.details').text(),
							status : $(this).attr('data-status')
						}); 
						me_order_inf.addEventListener('loaded', function (){
							me_order_inf.show('pop-in');
							plus.nativeUI.closeWaiting();
						})	
					})
				})
			}
		},
		error : function(e){
			
			console.log(JSON.stringify(e));
			
			plus.nativeUI.closeWaiting();
			
			toast('连接超时,请检查网络');
			
		}
	});
	
}


/**
 * 取消订单
 * @param {Object} id
 */
function cancel(id){
	plus.nativeUI.confirm('温馨提示: 确定要取消该订单吗?' , function(e){
		if(e.index == 0){
			plus.nativeUI.showWaiting('取消中...');
			$.ajax({
				type:"post",
				url:apiRoot +'/Home/Orders/cancel_order/', 
				data : {
					id : id
				},
				success : function(data){
					plus.nativeUI.closeWaiting(); 
					if(data == 1){
						$('.status'+id).text('订单已取消');
						$('.order'+id).attr('data-status' , 0);
						$('.order_status'+id).html('<button class="background-zx color-white order-btn">已取消</button></span></li></div>');
						toast('订单已取消');
					}
				},
				error : function(e){
					
					console.log(JSON.stringify(e));
					
					plus.nativeUI.closeWaiting();
					
					toast('连接超时,请检查网络');
					
				}
			});
		}
		
	}, '第三代咨询' ,['确定' , '取消']);
	
}
